package com.cx.controller;

import com.cx.service.GroupMemberService;
import com.cx.service.GroupSponsorService;
import com.cx.util.RequestResult;
import com.cx.vo.GroupMemberVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author: Zhou wei
 * @create: 2020/5/27 22:46
 */
@Controller
@RequestMapping("groupmember")
public class GroupMemberController {

    @Autowired
    GroupMemberService groupMemberService;

    @Autowired
    GroupSponsorService groupSponsorService;


    /**
     * 根据拼团id查找团内成员
     * @param groupSponsorId
     * @return
     */
    @RequestMapping("selectgroupmember")
    @ResponseBody
    public RequestResult selectGroupMemberByGroupSponsorId(Integer groupSponsorId){
        List<GroupMemberVO> groupMemberVOList = groupMemberService.selectGroupMemberByGroupSponsorId(groupSponsorId);
        return new RequestResult(1,"查询成功",groupMemberVOList);

    }



    /**
     * 加入某一拼团
     * @param userId
     * @param groupSponsorId
     * @return
     */
    @RequestMapping("joingroup")
    @ResponseBody
    @Transactional
    public RequestResult joinGroup(Integer userId,Integer groupSponsorId,Integer groupGoodId){

        Integer count = groupMemberService.countGroupByGoodId(userId, groupGoodId);
        if (count > 0){
            return new RequestResult(-1,"该商品您有正在进行中的拼团，不可再加入",null);
        }

        int affectedRows = groupMemberService.joinGroup(userId,groupSponsorId);

        if (affectedRows > 0){
            int updateJoinNum = groupSponsorService.updateJoinNum(groupSponsorId);
            if (updateJoinNum > 0){
                return new RequestResult(1,"加入拼团成功",null);
            } else {
                return new RequestResult(2,"加入拼团失败",null);
            }

        } else {
            return new RequestResult(2,"加入拼团失败",null);
        }
    }
}
